if (!Date.now) {
    Date.now = function() { return new Date().getTime(); };
}


var currentTime = Date.now() || +new Date();
var qs = getQueryStrings();
var myParam = qs["id"];
var mySource = qs["source"];
var myHash = qs["hash"];

loadVideoAds(myParam, mySource);

function getQueryStrings() { 
  var assoc  = {};
  var decode = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); };
  var queryString = location.search.substring(1); 
  var keyValues = queryString.split('&'); 

  for(var i in keyValues) { 
    var key = keyValues[i].split('=');
    if (key.length > 1) {
      assoc[decode(key[0])] = decode(key[1]);
    }
  } 

  return assoc; 
} 


//var qs = getQueryStrings();


//var first = getUrlVars()["me"];

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

// http://jquery-howto.blogspot.in/2009/09/get-url-parameters-values-with-jquery.html


function loadVideoAds(id, source){

    if(source==null)
	{
	 //loadJS('//html5.svnlabs.com/html5json.php?id='+myParam+'&t='+currentTime+'&_callback=processLast',false);
	 loadJS('http://html5player.svnlabs.com/jdata/'+myParam+'.json?hash='+currentTime+'&_callback=processLast',false);
	}
	else
	{
	 
	 if(source=="default")
	  loadJS('http://html5player.svnlabs.com/json/'+myParam+'.json?hash='+currentTime+'&_callback=processLast',false);
	 else
	  loadJS('http://html5player.svnlabs.com/json/'+myParam+'.json?hash='+currentTime+'&_callback=processMore',false);
	  
	}
	 //loadJS('http://html5player.svnlabs.com/json/'+myParam+'.json?hash='+currentTime+'&_callback=processLast',false);
	
}

function loadJS(url,cache){
    //cache is true by default
    var s = document.createElement('script');
    s.type = 'text/javascript';
    s.src=url;
    if (cache==false) {
        s.src+='&jpt='+ (+new Date());
    }
    document.getElementsByTagName('head')[0].appendChild(s);
}


function processMore(feed){
	
	loadJS('//html5.svnlabs.com/html5json-'+feed.sourcetype+'.php?id='+feed.id+'&sourceurl='+feed.params.sourceurl+'&t='+currentTime+'&_callback=processLast',false);
	
}


function processLast(feed){
	
	//console.log(feed.playlist.tracks.length);
	
	var str = '';
	
	for (i = 0; i < feed.playlist.tracks.length; i++) {
	
	str += '<li>';
    str += '<a href="#" data-src="'+feed.playlist.tracks[i].source+'" title="'+feed.playlist.tracks[i].title+'" class="track-name" name="'+feed.playlist.tracks[i].artwork+'">'+feed.playlist.tracks[i].title+'</a>';
    str += '<p class="Title01">'+feed.playlist.tracks[i].artist+'</p>';
    str += '</li>';
	
	}
	
	document.getElementById("tracks-list").innerHTML=str;
	
	
	var head1 = document.getElementsByTagName('head')[0];
	var script1 = document.createElement('script');
	script1.type = 'text/javascript';
	script1.src = '//html5mp3player.googlecode.com/svn/trunk/js/core-new.js';
	head1.appendChild(script1);
	
	$('#scrollbar1').tinyscrollbar_update(0);
	
	
}	



